package com.hndzyu.yd.vip;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.hndzyu.yd.R;
import com.hndzyu.yd.bean.GameList;
import com.hndzyu.yd.bean.UserBean;
import com.hndzyu.yd.utils.Util;
import com.zhy.view.flowlayout.FlowLayout;
import com.zhy.view.flowlayout.TagAdapter;
import com.zhy.view.flowlayout.TagFlowLayout;

import java.util.List;

/**
 * Created by zz9527 on 2018/12/4.
 */
public class GameListAdapter extends BaseAdapter {
    private  LayoutInflater inflater;
    private List<GameList.DataBean.ListBean> list;
    private Context context;
    public GameListAdapter(Context context,List<GameList.DataBean.ListBean> list) {
        this.list=list;
        this.context=context;
        inflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int i) {
        return list.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        final ViewHold hold;
        if (view==null){
            view=inflater.inflate(R.layout.adapter_game,null);
            hold=new ViewHold();
            hold.title=view.findViewById(R.id.game_title);
            hold.iv=view.findViewById(R.id.game_iv);
            hold.fl=view.findViewById(R.id.flowlayout);
            hold.price=view.findViewById(R.id.game_price);
            view.setTag(hold);
        }else {
            hold= (ViewHold) view.getTag();
        }
        GameList.DataBean.ListBean bean = list.get(i);
        hold.title.setText(bean.getAppName());
        if (UserBean.level>0 ){
            hold.price.setText("+" + Util.Sub(Double.parseDouble(bean.getMtTaskFeeSum())*0.9*(Double.parseDouble(UserBean.vipdivide)+Double.parseDouble(UserBean.divide))+"") +"元");
        }else{
            hold.price.setText("+" + Util.Sub(Double.parseDouble(bean.getMtTaskFeeSum())*0.9*Double.parseDouble(UserBean.divide)+"")+"元");
        }
        Glide.with(context).load(bean.getLogo()).into(hold.iv);
        hold.fl.setAdapter(new TagAdapter<String>(bean.getLabel())
        {
            @Override
            public View getView(FlowLayout parent, int position, String s)
            {
                TextView tv = (TextView) inflater.inflate(R.layout.tv,
                        hold.fl, false);
                tv.setText(s);
                return tv;
            }
        });
        return view;
    }
    class ViewHold{
        TextView title;
        ImageView iv;
        TagFlowLayout fl;
        TextView price;
    }
}
